#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'

from datetime import datetime

from ..extensions import db


class Settings(db.Model):
    '''一些设置信息'''
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    value = db.Column(db.String(200))
    type = db.Column(db.String(20))
    is_use = db.Column(db.Integer, default=1)
    seq = db.Column(db.Integer, default=1)
    icon = db.Column(db.String(50))

    # ：获取admin的二级菜单
    @classmethod
    def admin_second_bar(cls):
        list_bar = Settings.query.filter(Settings.type == 'admin_second_bar', Settings.is_use == 1).order_by(
            Settings.seq)
        return list_bar

    # ：获取名称
    @classmethod
    def blog_name(cls):
        blog_name = Settings.query.filter(Settings.type == 'blog_name', Settings.is_use == 1).first()
        if blog_name:
            return blog_name.name
        else:
            return 'golsee'

    # ：获取一级菜单
    @classmethod
    def first_bar(cls):
        first_bar = Settings.query.filter(Settings.type == 'first_bar', Settings.is_use == 1).order_by(Settings.seq)
        return first_bar

    # ：获取二级菜单
    @classmethod
    def blog_setting(cls):
        cls.query
        d = dict()
        settings = Settings.query.filter(Settings.seq == 0, Settings.is_use == 1).all()
        for setting in settings:
            d[setting.type] = setting.value
        return d


class Blog_info(db.Model):
    '''blog信息汇总'''
    date = db.Column(db.String(10), primary_key=True, index=True)
    visit_all = db.Column(db.Integer, default=0)
    visit_day = db.Column(db.Integer, default=0)
    visit_month = db.Column(db.Integer, default=0)
    visit_attack = db.Column(db.Integer, default=0)
    visit_attack_day = db.Column(db.Integer, default=0)
    visit_robot = db.Column(db.Integer, default=0)
    visit_robot_day = db.Column(db.Integer, default=0)
    article_all = db.Column(db.Integer, default=0)
    article_month = db.Column(db.Integer, default=0)
    user_all = db.Column(db.Integer, default=0)
    login_all = db.Column(db.Integer, default=0)
    comment_day = db.Column(db.Integer, default=0)
    comment_month = db.Column(db.Integer, default=0)
    comment_all = db.Column(db.Integer, default=0)

    def __init__(self):
        pass

    def __repr__(self):
        return '<Date: %s>' % self.date

    # ：根据日期返回记录,没有就新增
    @classmethod
    def get_info_by_day(cls, date):
        info = Blog_info.query.order_by(Blog_info.date.desc()).first()
        if info is None:
            info = Blog_info()
            info.date = date
            db.session.add(info)
            db.session.commit()
            info = Blog_info.query.filter_by(date=date).first()
        else:
            if info.date < date:
                new = Blog_info()
                new.date = date
                new.comment_all = info.comment_all
                new.visit_all = info.visit_all
                new.visit_attack = info.visit_attack
                new.visit_robot = info.visit_robot
                new.article_all = info.article_all
                new.user_all = info.user_all
                new.login_all = info.login_all
                if info.date[5:7] == date[5:7]:
                    new.visit_month = info.visit_month
                    new.article_month = info.article_month
                    new.comment_month = info.comment_month
                db.session.add(new)
                db.session.commit()
                info = Blog_info.query.filter_by(date=date).first()
            elif info.date > date:
                info = Blog_info.get_info_by_day(date)

        return info

    # ：获取最新信息
    @classmethod
    def info(cls):
        old = Blog_info.query.order_by(Blog_info.date.desc()).first()
        return old

    # ：正常访问+1
    @classmethod
    def new_visit(cls, date):
        info = Blog_info.get_info_by_day(date)
        info.visit_day += 1
        if info.date[5:7] == str(datetime.now().date())[5:7]:
            info.visit_month += 1
        else:
            info.visit_month = 1
        info.visit_all += 1
        db.session.add(info)
        db.session.commit()

    # ：机器人访问+1
    @classmethod
    def new_robot_visit(cls, date):
        info = Blog_info.get_info_by_day(date)
        info.visit_robot += 1
        info.visit_robot_day += 1
        db.session.add(info)
        db.session.commit()

    # ：疑似攻击+1
    @classmethod
    def new_attack_visit(cls, date):
        info = Blog_info.get_info_by_day(date)
        info.visit_attack += 1
        info.visit_attack_day += 1
        db.session.add(info)
        db.session.commit()

    # ：文章+1
    @classmethod
    def new_article(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.article_all += 1
        old.article_month += 1
        db.session.add(old)
        db.session.commit()

    #: 用户+1
    @classmethod
    def new_user(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.user_all += 1
        db.session.add(old)
        db.session.commit()

    #: 登陆+1
    @classmethod
    def new_login(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.login_all += 1
        db.session.add(old)
        db.session.commit()

    #: 评论+1
    @classmethod
    def new_comment(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.comment_day += 1
        old.comment_month += 1
        old.comment_all += 1
        db.session.add(old)
        db.session.commit()

    #: 用户-1
    @classmethod
    def del_user(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.user_all -= 1
        db.session.add(old)
        db.session.commit()

    #: 文章-1
    @classmethod
    def del_article(cls):
        old = Blog_info.get_info_by_day(str(datetime.now().date()))
        old.article_all -= 1
        db.session.add(old)
        db.session.commit()
